﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using System.Text;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model.Calendar;
using System.Reflection;
using Cn.Cellula.Client;
using Cn.Cellula.Biz.Def;
using Cn.Edwon.Util;
using Cn.Cellula.Client.ControlHelper;
using Cn.Edwon.Web;
using Cn.Cellula.Model.Security;

namespace Cn.Cellula.Website.Admin.Calendar {
    public partial class List : PageSupport {
        ICalendarBiz biz = BizFactory.Get<ICalendarBiz>();
        DateTime fromDt = DateTime.Now;
        DateTime toDt = DateTime.Now;
        String userKey = "";
        protected void Page_Load(object sender, EventArgs e) {
            userKey = Request.QueryString["uid"];
            try {
                if (!String.IsNullOrEmpty(Request.QueryString["dt1"])) {
                    fromDt = Convert.ToDateTime(Request.QueryString["dt1"]);
                }
                if (!String.IsNullOrEmpty(Request.QueryString["dt2"])) {
                    toDt = Convert.ToDateTime(Request.QueryString["dt2"]);
                }
            } catch {
                fromDt = DateTime.Now;
                toDt = DateTime.Now;
            }
            if (!Page.IsPostBack) {
                InitForm();
                LoadEOList();
            }
        }
        private void LoadEOList() {
            IList<VCalendarTodoEO> list = new List<VCalendarTodoEO>();
            DateTime from = Convert.ToDateTime(this.txt_FromDt.Text);
            if (String.IsNullOrEmpty(this.hid_UserKey.Value)) {//显示全部
                list = biz.LoadCalendarTodo(this.OwnerKey, this.Actor.ID, this.txt_FromDt.Text+" 0:0:0", this.txt_ToDt.Text+" 23:59:59", TodoStatus.All);
            } else {
                list = biz.LoadCalendarTodo(this.OwnerKey, this.hid_UserKey.Value, this.txt_FromDt.Text + " 0:0:0", this.txt_ToDt.Text + " 23:59:59", TodoStatus.All);
            }
            this.gvTodo.DataSource = list;
            this.gvTodo.DataBind();
        }

        private void InitForm() {
            if (!String.IsNullOrEmpty(userKey)) {
                TSysUserEO user = biz.Load<TSysUserEO>(userKey);
                this.txt_UserName.Text = user.UserName;
                this.hid_UserKey.Value = user.UserKey;
            }
            this.txt_FromDt.Text = fromDt.ToString("yyyy-MM-dd");
            this.txt_ToDt.Text = toDt.ToString("yyyy-MM-dd");
        }

        protected void btn_EOAdd_Click(object sender, EventArgs e) {
            Response.Redirect("EditTodo.aspx?rd=1&dt=" + Request.QueryString["dt"]);
        }

        protected void btn_EOUpdate_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvTodo, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                Response.Redirect("EditTodo.aspx?rd=1&dt=" + Request.QueryString["dt"] + "&id=" + keys[0]);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoEditSelect);
                return;
            }
        }

        protected void btn_EODel_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvTodo, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                TCalendarTodoEO eo = null;
                foreach (String k in keys) {
                    eo = biz.Load<TCalendarTodoEO>(k);
                    if (eo.IsLocked == 1) continue;
                    biz.Delete<TCalendarTodoEO>(k);
                }
                BizFactory.Get<ILogBiz>().LogDelete("待办事项", keys.Count, this.Actor, Request);
                this.LoadEOList();
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
        }

        protected void gvItem_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            this.gvTodo.PageIndex = e.NewPageIndex;
            LoadEOList();
        }

        protected void btn_Query_Click1(object sender, EventArgs e) {
            LoadEOList();
        }
    }
}